﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ThingyCloudWPF.Creatures;

namespace ThingyCloudWPF.Graphics
{
	/// <summary>
	/// Interaction logic for Thingy.xaml
	/// </summary>
	public partial class ThingyGraphic : UserControl
	{
		private MainWindow mainwin;
		private Thingy thingy;

		public ThingyGraphic(MainWindow mainwin, Thingy t)
		{
			InitializeComponent();

			this.mainwin = mainwin;
			thingy = t;

			Loaded += new RoutedEventHandler(ThingyGraphic_Loaded);
		}

		void ThingyGraphic_Loaded(object sender, RoutedEventArgs e)
		{
			string[] files = Directory.GetFiles("C:/dev/ThingyCloudWPF/ThingyCloudWPF/Images/Thingies/");
			Random r = new Random(DateTime.Now.Millisecond);
			graphic.Source = new BitmapImage(new Uri("file://" + files[r.Next(0, files.Length-1)]));
		}

		public int ThingyID
		{
			get
			{
				return thingy.ID;
			}
		}

		public bool IsGraphicFor(Thingy t)
		{
			return thingy.ID == t.ID;
		}

		public void Update()
		{
			Margin = new Thickness(
				mainwin.ToWindowX(thingy.GetBoundingRect().X),
				mainwin.ToWindowY(thingy.GetBoundingRect().Y),
				0,
				0);

			LayoutTransform = new RotateTransform(360 - thingy.Direction);

			UpdateLayout();
		}
	}
}
